//
//  NSS.swift
//  Store
//
//  Created by  on 2022/9/17.
//

import UIKit
import CommonCrypto

extension NSString{
    @objc func size(_ font:UIFont, _ w:CGFloat)->CGSize{
        let size = CGSize(width:w, height: CGFloat(MAXFLOAT))
        let rect = self.boundingRect(with: size, options:[.usesFontLeading,.truncatesLastVisibleLine,.usesLineFragmentOrigin
                                                         ], attributes: [.font:font], context: nil)
        return CGSize(width: ceil(rect.size.width), height: ceil(rect.size.height))
    }
    
   @objc var md5: String
    {
        let str = self.cString(using: String.Encoding.utf8.rawValue)
        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8.rawValue))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        CC_MD5(str!, strLen, result)
        
        let hash = NSMutableString()
        for i in 0..<digestLen {
            hash.appendFormat("%02x", result[i])
        }
        
        result.deallocate()
        return hash as String
    }
}
